package com.jy.blog.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jy.blog.dao.ArticleDao;
import com.jy.blog.model.Article;
import com.jy.blog.utils.DBHelper;

public class ArticleDaoImpl  implements  ArticleDao{
	private Connection conn;
	private PreparedStatement pstm;
	private ResultSet rs;
	@Override
	public List<Article> selectAllArticle() {
		List<Article> list=new ArrayList<Article>();
		conn=DBHelper.getConnection();
		String sql="SELECT art_id,art_title,art_content,art_date,ad_photo,author FROM article";
		  try {
			pstm=conn.prepareStatement(sql);
			rs=pstm.executeQuery();
			while(rs.next()){
				Article article=new Article();
				article.setArtId(rs.getInt(1));
				article.setArtTitle(rs.getString(2));
				article.setArtContent(rs.getString(3));
				article.setArtDate(rs.getString(4));
				article.setAdPhoto(rs.getString(5));
				article.setAuthor(rs.getString(6));
				list.add(article);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBHelper.closeAll(pstm, rs);
		}
		  
		
		
		return list;
	}

	@Override
	public Article sleectById(int id) {
		Article article=new Article();
		conn=DBHelper.getConnection();
		String sql="SELECT art_id,art_title,art_content,art_date,ad_photo,author FROM article WHERE art_id=?";
		 try {
			pstm=conn.prepareStatement(sql);
			pstm.setInt(1, id);
			rs=pstm.executeQuery();
			if(rs.next()){
				article.setArtId(rs.getInt(1));
				article.setArtTitle(rs.getString(2));
				article.setArtContent(rs.getString(3));
				article.setArtDate(rs.getString(4));
				article.setAdPhoto(rs.getString(5));
				article.setAuthor(rs.getString(6));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBHelper.closeAll(pstm, rs);
			}
		 
		return article;
	}

	@Override
	public int add(Article article) {
		 int result=0;
		 conn=DBHelper.getConnection();
		 String sql="INSERT INTO article VALUES(NULL,?,?,?,?,?)";
		 try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, article.getArtTitle());
			pstm.setString(2, article.getArtContent());
			pstm.setString(3, article.getArtDate());
			pstm.setString(4, article.getAdPhoto());
			pstm.setString(5, article.getAuthor());
			result=pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBHelper.closeAll(pstm, rs);
		}
		return result;
	}

	@Override
	public int update(Article article) {
		// TODO Auto-generated method stub
		int result=0;
		conn=DBHelper.getConnection();
		String sql="UPDATE article SET art_title=?,art_content=?,art_date=?,ad_photo=? WHERE art_id=?";
		   try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, article.getArtTitle());
			pstm.setString(2, article.getArtContent());
			pstm.setString(3, article.getArtDate());
			pstm.setString(4, article.getAdPhoto());
			pstm.setInt(5, article.getArtId());
			result=pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBHelper.closeAll(pstm, rs);
		}
		return result;
	}

	@Override
	public int del(int id) {
		int result=0;
		conn=DBHelper.getConnection();
		String sql="DELETE FROM article WHERE art_id=?";
		try {
			pstm=conn.prepareStatement(sql);
			pstm.setInt(1, id);
			result=pstm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBHelper.closeAll(pstm, rs);
		}
		
		return result;
		}

	@Override
	public List<Article> getProduct(String name) {
		List<Article> kList=new ArrayList<Article>();
		conn=DBHelper.getConnection();
		String sql="SELECT * FROM article WHERE author like=?";
		try {
			pstm=conn.prepareStatement(sql);
			pstm.setString(1, "%"+name+"%");
			rs=pstm.executeQuery();
			while(rs.next()){
				Article p=new Article();
				p.setArtId(rs.getInt(1));
				p.setArtTitle(rs.getString(2));
				p.setArtContent(rs.getString(3));
				p.setAuthor(rs.getString(4));
				p.setArtDate(rs.getString(5));
				kList.add(p);
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}finally{
			DBHelper.closeAll(pstm, rs);
		}
		return  kList;
	}


}
